package com.cheer.dto;

import com.cheer.entity.Applicant;
import com.cheer.entity.Organization;
import com.cheer.enums.RegisterStatusEnum;
import com.cheer.enums.ReviewTypeEnum;
import com.cheer.enums.RoleEnum;
import com.cheer.enums.TitleEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Data
@Accessors(chain = true)
public class RegisterDTO {

    @NotBlank(message = "集体名称不能为空")
    @Length(max = 50, message = "集体名称不能超过50个字符")
    private String applicantName;

    @NotNull(message = "文明号状态不能为空")
    private Boolean awarded;

    private Integer title;

    @Length(max = 4, message = "获得年份不能超过4个字符")
    private String titleYear;

    @NotBlank(message = "手机号不能为空")
    @Length(max = 20, message = "手机号不能超过20个字符")
    private String phoneAccount;

    @Length(max = 20, message = "备用账号不能超过20个字符")
    private String backupAccount;

    @NotBlank(message = "密码不能为空")
    @Length(max = 20, message = "密码不能超过20个字符")
    private String password;

    @NotBlank(message = "确认密码不能为空")
    @Length(max = 20, message = "确认密码不能超过20个字符")
    private String confirmPassword;

    @NotBlank(message = "设置问题不能为空")
    @Length(max = 200, message = "设置问题不能超过200个字符")
    private String securityQuestion;

    @NotBlank(message = "设置答案不能为空")
    @Length(max = 200, message = "设置答案不能超过200个字符")
    private String securityAnswer;

    @NotNull(message = "组织机构不能为空")
    private Long orgId;

    private Integer reviewType;

    private Long joinReviewOrgId;

    @NotBlank(message = "集体负责人不能为空")
    @Length(max = 20, message = "集体负责人不能超过20个字符")
    private String contactPerson;

    @NotBlank(message = "集体负责人电话不能为空")
    @Length(max = 20, message = "集体负责人电话不能超过20个字符")
    private String contactPersonPhone;

    @NotBlank(message = "集体负责人职务不能为空")
    @Length(max = 200, message = "集体负责人职务不能超过200个字符")
    private String contactPersonPost;

    @NotNull(message = "集体负责人出生年月不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date contactPersonBirth;

    @NotNull(message = "职工人数不能为空")
    @Range(min = 6, max = 200, message = "职工人数的范围为 6-200 人")
    private Integer personNumber;

    @NotNull(message = "35周岁以下青年人数不能为空")
    private Integer youthNumber;

    @Length(max = 20, message = "集体电话不能超过20个字符")
    private String phone;

    @Length(max = 50, message = "邮箱不能超过50个字符")
    private String email;

    @NotBlank(message = "地址不能为空")
    @Length(max = 200, message = "地址不能超过200个字符")
    private String address;

    private String checkDoc;

    private String usedName;

    @Length(max = 2000, message = "备注不能超过2000个字符")
    private String remark;

    @NotBlank(message = "验证码不能为空")
    private String captcha;

    public AccountDTO build() {
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setPhone(phoneAccount);
        accountDTO.setBackupAccount(backupAccount);
        accountDTO.setPassword(password);
        accountDTO.setRole(RoleEnum.APPLICANT.getCode());
        accountDTO.setSecurityQuestion(securityQuestion);
        accountDTO.setSecurityAnswer(securityAnswer);
        return accountDTO;
    }

    public Applicant build(Long accountId, String orgPath, Organization joinOrg) {
        Applicant applicant = new Applicant();
        applicant.setAccountId(accountId);
        applicant.setRegisterStatus(RegisterStatusEnum.REGISTER.getCode());
        applicant.setApplicantName(applicantName);
        if (awarded) {
            applicant.setRegisterTitle(title);
            applicant.setTitleYear(titleYear);
        } else {
            applicant.setRegisterTitle(TitleEnum.NO.getType());
            applicant.setTitleYear(StringUtils.EMPTY);
        }
        applicant.setTitle(TitleEnum.NO.getType());
        applicant.setOrgId(orgId);
        applicant.setOrgPath(orgPath);
        applicant.setReviewType(reviewType);
        if(ReviewTypeEnum.isIndependent(orgPath)){
            applicant.setReviewType(ReviewTypeEnum.INDEPENDENT.getCode());
        }
        if (joinOrg != null) {
            applicant.setJoinReviewOrgId(joinOrg.getId());
            applicant.setJoinReviewOrgPath(joinOrg.getOrgPath());
        }
        applicant.setCheckDoc(checkDoc);
        applicant.setUsedName(usedName);
        applicant.setContactPerson(contactPerson);
        applicant.setContactPersonPhone(contactPersonPhone);
        applicant.setContactPersonPost(contactPersonPost);
        applicant.setContactPersonBirth(contactPersonBirth);
        applicant.setPersonNumber(personNumber);
        applicant.setYouthNumber(youthNumber);
        applicant.setPhone(phone);
        applicant.setEmail(email);
        applicant.setAddress(address);
        applicant.setRemark(remark);
        return applicant;
    }
}
